package com.sky.service.impl;


import com.alibaba.druid.sql.visitor.functions.If;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 菜品管理实现类
 */
@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品
     * @param dishDTO
     */
    @Transactional
    public void save(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        List<DishFlavor> collectFlavors = flavors.stream().map((item) -> {
            item.setDishId(dish.getId());
            return item;
        }).collect(Collectors.toList());
        collectFlavors.forEach((item) -> {
            dishFlavorMapper.insert(item);
        });
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        if(dishPageQueryDTO.getName()!=null||dishPageQueryDTO.getStatus()!=null){
            PageHelper.startPage(1, 10);
            Page<Dish> page = dishMapper.pageQuery(dishPageQueryDTO);
            long total = page.getTotal();
            List<Dish> records = page.getResult();
//       log.info("分页查询菜品信息，结果：{}",records);
            //转换类型，给categoryName赋值
            List<DishVO> collect = records.stream().map((item) -> {
                DishVO dishVO = new DishVO();
                BeanUtils.copyProperties(item,dishVO);
                Category category = categoryService.getCategoryById(item.getCategoryId());
                dishVO.setCategoryName(category.getName());
                return dishVO;
            }).collect(Collectors.toList());
            return new PageResult(total, collect);
        }
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<Dish> page = dishMapper.pageQuery(dishPageQueryDTO);
        long total = page.getTotal();
        List<Dish> records = page.getResult();
//       log.info("分页查询菜品信息，结果：{}",records);
        //转换类型，给categoryName赋值
        List<DishVO> collect = records.stream().map((item) -> {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(item,dishVO);
            Category category = categoryService.getCategoryById(item.getCategoryId());
            dishVO.setCategoryName(category.getName());
            return dishVO;
        }).collect(Collectors.toList());
        return new PageResult(total, collect);
    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    @Transactional
    public void update(DishDTO dishDTO) {
        if (dishDTO != null){
            Dish dish=new Dish();
            BeanUtils.copyProperties(dishDTO,dish);
            dishMapper.update(dish);
            List<DishFlavor> flavors = dishDTO.getFlavors();
            //全部删除
            if(flavors != null){
                Long dishId = flavors.get(0).getDishId();
                dishFlavorMapper.deleteByDishId(dishId);
            }
            //重新填充
            for (DishFlavor item : flavors){
//                dishFlavorMapper.updateById(item);
                dishFlavorMapper.insert(item);
            }

        }


    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    public DishVO getById(Long id) {
        if (id != null){
            Dish dish = dishMapper.getById(id);
            List<DishFlavor> dishFlavorList = dishFlavorMapper.getByDishId(id);
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish,dishVO);
            dishVO.setFlavors(dishFlavorList);
            return dishVO;
        }
        return null;
    }

    /**
     * 菜品起售停售
     * @param status
     * @param id
     */
    public void startOrStop(Integer status, Long id) {
        if(id != null){
            Dish dish =dishMapper.getById(id);
            dish.setStatus(dish.getStatus()==1?0:1);
            dishMapper.startOrStop(dish);
        }
    }

    /**
     * 批量删除
     * @param ids
     */
    @Transactional
    public void deleteIds(List<Long> ids) {

        if (ids != null){
            for (Long id : ids){
                dishMapper.deleteById(id);
            }
        }
    }

    @Override
    public List<Dish> list(Long categoryId) {
        if (categoryId != null){
            return dishMapper.list(categoryId);
        }
        return null;
    }
}
